package net.begincode.core.service;

import net.begincode.searchCommon.bean.Page;
import net.begincode.searchCommon.util.JSONUtil;
import net.begincode.searchCommon.util.HttpUtil;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.net.URISyntaxException;

/**
 * @author hlj~宇晗
 * @create 2017-03-24 21:16
 **/
//业务逻辑层
@Service
public class SearchService {
	private static CloseableHttpResponse httpResponse;
	public static Page search(String status,String keyword,int page) throws URISyntaxException, IOException {
			//重新连接机制，count为当前连接次数,限制最大连接次数
		    for(int count=1;count<4;count++) {
				httpResponse =
						HttpUtil.doGet(HttpUtil.getURI(status, keyword,count,page));
				if (httpResponse.getStatusLine().getStatusCode() == 200) {
					break;
				} else {
					continue;
				}
			}
				HttpEntity httpEntity = httpResponse.getEntity();
				//进行设置page所有的参数，构建page
		        Page pageResult=JSONUtil.setAllParam(httpEntity);
				httpResponse.close();
			return pageResult;
	}
}
